#include "widget.h"
#include "ui_widget.h"
#include <QTreeWidgetItem>
#include <QTreeWidget>
#include <QIcon>
#include <QDebug>
#include<QTimer>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    //C:\Users\chenbinbin\AppData\Roaming\kingsoft\office6\configs\appicon
    ui->setupUi(this);

    // 树应用
    // 设置水平头
    ui->treeWidget->setHeaderLabels(QStringList()<<"英雄"<<"英雄介绍");

    QTreeWidgetItem *liitem = new QTreeWidgetItem(QStringList()<<"力量");    
    QTreeWidgetItem *minitem = new QTreeWidgetItem(QStringList()<<"敏捷");
    QTreeWidgetItem *zhiitem = new QTreeWidgetItem(QStringList()<<"智力");
    eye[0] = new QIcon("D:/selfad/qtapp/06_QtTreeWidget/eys.svg");
    eye[1] =  new QIcon("D:/selfad/qtapp/06_QtTreeWidget/eysoff.svg");
//    eye.addFile("D:/selfad/qtapp/06_QtTreeWidget/eys.svg", QSize(), QIcon::Normal, QIcon::On);
//    eye.addFile("D:/selfad/qtapp/06_QtTreeWidget/eysoff.svg", QSize(), QIcon::Normal, QIcon::Off);
    liitem->setIcon(0, *eye[0]);
    // liitem->setIcon(0, eyeoff);

    // 加载顶层节点
    ui->treeWidget->addTopLevelItem(liitem);
    ui->treeWidget->addTopLevelItem(minitem);
    ui->treeWidget->addTopLevelItem(zhiitem);

    // 添加英雄
    QStringList heroL1;
    heroL1 << "刚被猪" << "前排坦克，口中中";
    QTreeWidgetItem *l1 = new QTreeWidgetItem(heroL1);
    liitem->addChild(l1);

//    QTimer::singleShot(5000,this, [&](){

//        qDebug()<<"time out";
//        liitem->setIcon(0, *eye[1]);
//    });



    int s = 0;
    connect(ui->treeWidget, &QTreeWidget::itemClicked, this, [&](QTreeWidgetItem *item, int column){
//            if(index.column() == 0){
//                QTreeWidgetItem *item = treeWidget.itemFromIndex(index);
//                if(item->text(0) == "Example Item"){ // 检查是不是我们想要切换图标的项
//                    item->setExpanded(true); // 确保项目展开
//                    item->setCheckState(0, item->checkState(0) == Qt::Checked ? Qt::Unchecked : Qt::Checked);
//                }
//            }
        qDebug()<<"clicked";
        qDebug()<<item->text(column);
        // item->set
        if(column==0) {
            item->setIcon(column, *eye[s^1]);
            // item->setText(0, "666");
            s^=1;
        }
    });

}

Widget::~Widget()
{
    delete ui;
}

